<?php $seller = \Session::get('seller', false); ?>
<style type="text/css">
.subject{
    font-size: 17px;
    font-weight: 600;
    color: #aaa;
    margin: 0px 0px 0px 45px;
}
.hr{
    width: 90%; 
    margin-top: 10px;
    margin-bottom: 10px;
}
.box {
    position: relative;
    padding: 45px 50px;
    margin: 0 -15px 15px;
    border-style: solid;
    margin-right: 0;
    margin-left: 0;
    background-color: #fff;
    border-color: #ddd;
    border-width: 1px;
    border-radius: 4px 4px 0 0;
    -webkit-box-shadow: none;
    box-shadow: none;
}
.images{
    list-style: none;
    margin-left: 0px;
    padding-left: 0px;
}
.images li{
    margin-left: 10px;
    float: left;
}
.talbe{
    font-family: "微软雅黑, sans-serif"
}
.table .active th, .table .active td{
    font-size: 14px;
    font-weight: 600;
}
.table th, .table td{
    font-size: 14px;
    font-weight: 100;
}
</style>
<div class="row">
    <div class="col-xs-1"></div>
    <div class="col-xs-10">
        <div class="box">
            <?php echo render('ace/public/alert_message'); ?>
            <form class="form-horizontal" role="form" method="post" action="/admin/car/save/<?php echo isset($item) && $item ? $item->id : ''; ?>">
                <?php echo \Form::csrf(); ?>
                <input type="hidden" id="options" name="options" value=""/>
                <input type="hidden" id="features" name="feature_data" value=""/>
                <input type="hidden" id="images" name="images_data" value=""/>
                <div class="form-group">
                    <label for="title">汽车名称</label>
                    <input type="text" class="form-control" id="title" name="title" placeholder="汽车名称" value="<?php echo isset($item) && $item ? $item->title : ''; ?>">
                </div>
                <div class="form-group">
                    <label for="title_en">英文名称</label>
                    <input type="text" class="form-control" id="title_en" name="title_en" placeholder="英文名称" value="<?php echo isset($item) && $item ? $item->title_en : ''; ?>">
                </div>
                <div class="form-group">
                    <label for="title">二级名称</label>
                    <input type="text" class="form-control" id="sub_title" name="sub_title" placeholder="二级名称" value="<?php echo isset($item) && $item ? $item->sub_title : ''; ?>">
                </div>
                <div class="form-group">
                    <label for="category_id">车型系列</label>
                    <div class="input-group">
                        <select class="form-control"  id="category_id" name="category_id">
                        
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="thumbnail">缩略图</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="thumbnail" name="thumbnail" placeholder="列表页显示的图片" value="<?php echo isset($item) && $item ? $item->thumbnail : ''; ?>">
                        <span class="input-group-addon" id="basic-addon2"><a data-panel='thumbnail' data-toggle="modal" data-target="#modal-upload"><i class="fa fa-upload"></i></a></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="master_image">主图</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="master_image" name="master_image" placeholder="详情页显示的第一张图" value="<?php echo isset($item) && $item ? $item->master_image : ''; ?>">
                        <span class="input-group-addon" id="basic-addon2"><a data-panel='master_image' data-toggle="modal" data-target="#modal-upload"><i class="fa fa-upload"></i></a></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="body">介绍</label>
                    <textarea class="form-control" id="body" name="body" placeholder="资讯内容" role="editor" style="height:300px;"><?php echo isset($item) && $item ? $item->body : ''; ?></textarea>
                </div>
                <div class="form-group">
                    <div role="tabpanel">
                        <!-- Nav tabs -->
                        <ul class="nav nav-tabs" role="tablist">
                            <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">车型图库</a></li>
                            <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">特性</a></li>
                            <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">参数配置</a></li>
                            <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">视频音频</a></li>
                        </ul>

                        <!-- Tab panes -->
                        <div class="tab-content" style="border: 0px 1px 1px 1px solid #e0e0e0;">
                            <div role="tabpanel" class="tab-pane active" id="home" style="overflow: auto;">
                                <div class="col-xs-12">
                                    <!-- PAGE CONTENT BEGINS -->
                                    <a class="btn btn-primary" id="btnAddImage" data-toggle="modal" data-target="#modal-upload"><i class="fa fa-plus"></i></a>
                                    <div>
                                        <ul class="ace-thumbnails clearfix">
                                        </ul>
                                    </div><!-- PAGE CONTENT ENDS -->
                                </div>
                            </div>
                            <div role="tabpanel" class="tab-pane" id="profile" style=" padding-top: 10px;">
                                <ul class="nav nav-tabs" role="tablist" id="features_nav">
                                </ul>
                                <div class="tab-content" style="border: 0px 1px 1px 1px solid #e0e0e0; padding-top: 10px;" id="features_content">
                                </div>
                            </div>
                            <?php 
                                $options = isset($item) && $item ? json_decode(htmlspecialchars_decode($item->options)) : false;
                            ?>
                            <div role="tabpanel" class="tab-pane" id="messages">
                                <table class="table table-bordered" style="margin-top: 10px;">
                                    <thead>
                                        <tr class="active">
                                            <th>车型参数</th>
                                            <td>车型参数值</td>
                                            <th>车型参数</th>
                                            <td>车型参数值</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <th>排量</th>
                                            <td><input type="text" id="displacement" value="<?php echo $options ? $options->overview->displacement : ''; ?>" role="overview"></td>
                                            <th>最高速度</th>
                                            <td><input type="text" id="top_speed" value="<?php echo $options ? $options->overview->top_speed : ''; ?>" role="overview"></td>
                                        </tr>
                                        <tr>
                                            <th>0至100公里每小时加速</th>
                                            <td><input type="text" id="km_h" value="<?php echo $options ? $options->overview->km_h : ''; ?>" role="overview"></td>
                                            <th>价格</th>
                                            <td><input type="text" id="price" value="<?php echo $options ? $options->overview->price : ''; ?>" role="overview"></td>
                                        </tr>
                                        <tr class="active">
                                            <th colspan="4">尺寸重量</th>
                                        </tr>
                                        <tr>
                                            <th>长</th>
                                            <td><input type="text" id="length" value="<?php echo $options ? $options->dimensions->length : ''; ?>" role="dimensions"></td>
                                            <th>宽（包括后视镜）</th>
                                            <td><input type="text" id="width_with_mirrors" value="<?php echo $options ? $options->dimensions->width_with_mirrors : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>高</th>
                                            <td><input type="text" id="height" value="<?php echo $options ? $options->dimensions->height : ''; ?>" role="dimensions"></td>
                                            <th>宽（不包括后视镜）</th>
                                            <td><input type="text" id="width_without_mirrors" value="<?php echo $options ? $options->dimensions->width_without_mirrors : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>轴距</th>
                                            <td><input type="text" id="Wheelbase" value="<?php echo $options ? $options->dimensions->Wheelbase : ''; ?>" role="dimensions"></td>
                                            <th>前轮距</th>
                                            <td><input type="text" id="front_track" value="<?php echo $options ? $options->dimensions->front_track : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>后轮距</th>
                                            <td><input type="text" id="rear_track" value="<?php echo $options ? $options->dimensions->rear_track : ''; ?>" role="dimensions"></td>
                                            <th>前悬</th>
                                            <td><input type="text" id="front_overhang" value="<?php echo $options ? $options->dimensions->front_overhang : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>后悬</th>
                                            <td><input type="text" id="rear_overhang" value="<?php echo $options ? $options->dimensions->rear_overhang : ''; ?>" role="dimensions"></td>
                                            <th>转弯直径</th>
                                            <td><input type="text" id="turning_circle" value="<?php echo $options ? $options->dimensions->turning_circle : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>行李箱容量</th>
                                            <td><input type="text" id="boot_capacity" value="<?php echo $options ? $options->dimensions->boot_capacity : ''; ?>" role="dimensions"></td>
                                            <th>燃油箱容积</th>
                                            <td><input type="text" id="fuel_tank_capacity" value="<?php echo $options ? $options->dimensions->fuel_tank_capacity : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>整备重量</th>
                                            <td><input type="text" id="Kerb_weight" value="<?php echo $options ? $options->dimensions->Kerb_weight : ''; ?>" role="dimensions"></td>
                                            <th>干重</th>
                                            <td><input type="text" id="gan_weight" value="<?php echo $options && isset($options->dimensions->gan_weight) ? $options->dimensions->gan_weight : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>整备重量+驾驶员</th>
                                            <td><input type="text" id="driver_weight" value="<?php echo $options && isset($options->dimensions->full_weight) ? $options->dimensions->driver_weight : ''; ?>" role="dimensions"></td>
                                            <th>满载重量</th>
                                            <td><input type="text" id="full_weight" value="<?php echo $options && isset($options->dimensions->full_weight) ? $options->dimensions->full_weight : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr>
                                            <th>车辆毛重</th>
                                            <td><input type="text" id="total_weight" value="<?php echo $options && isset($options->dimensions->total_weight) ? $options->dimensions->total_weight : ''; ?>" role="dimensions"></td>
                                            <th>重量分配</th>
                                            <td><input type="text" id="weight_fenpei" value="<?php echo $options && isset($options->dimensions->weight_fenpei) ? $options->dimensions->weight_fenpei : ''; ?>" role="dimensions"></td>
                                        </tr>
                                        <tr class="active">
                                            <th colspan="4">轮胎</th>
                                        </tr>
                                        <tr>
                                            <th>前轮胎尺寸</th>
                                            <td><input type="text" id="front_tyres" value="<?php echo $options ? $options->tyres->front_tyres : ''; ?>" role="tyres"></td>
                                            <th>后轮胎尺寸</th>
                                            <td><input type="text" id="rear_tyres" value="<?php echo $options ? $options->tyres->rear_tyres : ''; ?>" role="tyres"></td>
                                        </tr>
                                        <tr class="active">
                                            <th colspan="4">发动机</th>
                                        </tr>
                                        <tr>
                                            <th>气缸数量和布局</th>
                                            <td><input type="text" id="cylinders_and_layout" value="<?php echo $options ? $options->engine->cylinders_and_layout : ''; ?>" role="engine"></td>
                                            <th>排量</th>
                                            <td><input type="text" id="displacement" value="<?php echo $options ? $options->engine->displacement : ''; ?>" role="engine"></td>
                                        </tr>
                                        <tr>
                                            <th>缸径</th>
                                            <td><input type="text" id="gangjing" value="<?php echo $options && isset($options->engine->gangjing) ? $options->engine->gangjing : ''; ?>" role="engine"></td>
                                            <th>冲程</th>
                                            <td><input type="text" id="chongcheng" value="<?php echo $options && isset($options->engine->chongcheng) ? $options->engine->chongcheng : ''; ?>" role="engine"></td>
                                        </tr>
                                        <tr>
                                            <th>变速箱</th>
                                            <td><input type="text" id="transmission" value="<?php echo $options ? $options->engine->transmission : ''; ?>" role="engine"></td>
                                            <th>压缩比</th>
                                            <td><input type="text" id="compression" value="<?php echo $options ? $options->engine->compression : ''; ?>" role="engine"></td>
                                        </tr>
                                        <tr>
                                            <th>最大功率</th>
                                            <td><input type="text" id="max_power_output" value="<?php echo $options ? $options->engine->max_power_output : ''; ?>" role="engine"></td>
                                            <th>最大功率转速</th>
                                            <td><input type="text" id="max_speed" value="<?php echo $options ? $options->engine->max_speed : ''; ?>" role="engine"></td>
                                        </tr>
                                        <tr>
                                            <th>最大扭矩</th>
                                            <td><input type="text" id="peak_torque" value="<?php echo $options ? $options->engine->peak_torque : ''; ?>" role="engine"></td>
                                            <th>最大扭矩转速</th>
                                            <td><input type="text" id="engine_speed" value="<?php echo $options ? $options->engine->engine_speed : ''; ?>" role="engine"></td>
                                        </tr>
                                        <tr>
                                            <th>增压扭矩</th>
                                            <td><input type="text" id="overboost_torque" value="<?php echo $options ? $options->engine->overboost_torque : ''; ?>" role="engine"></td>
                                            <th>最大扭矩转速</th>
                                            <td><input type="text" id="engine_speed_overboost_torque" value="<?php echo $options ? $options->engine->engine_speed_overboost_torque : ''; ?>" role="engine"></td>
                                        </tr>
                                        <tr class="active">
                                            <th colspan="4">性能</th>
                                        </tr>
                                        <tr>
                                            <th>最高车速</th>
                                            <td><input type="text" id="maximum_speed" value="<?php echo $options ? $options->performance->maximum_speed : ''; ?>" role="performance"></td>
                                            <th>0至100公里/小时加速</th>
                                            <td><input type="text" id="acceleration_km_h" value="<?php echo $options ? $options->performance->acceleration_km_h : ''; ?>" role="performance"></td>
                                        </tr>
                                        <tr>
                                            <th>100公里/小时至0制动距离</th>
                                            <td><input type="text" id="stopping_distance" value="<?php echo $options ? $options->performance->stopping_distance : ''; ?>" role="performance"></td>
                                            <th>燃油消耗量（城市工况）</th>
                                            <td><input type="text" id="combined_cycle" value="<?php echo $options ? $options->performance->combined_cycle : ''; ?>" role="performance"></td>
                                        </tr>
                                        <tr>
                                            <th>燃油消耗量（市外工况）</th>
                                            <td><input type="text" id="urban_cycle" value="<?php echo $options ? $options->performance->urban_cycle : ''; ?>" role="performance"></td>
                                            <th>燃油消耗量（混合工况）</th>
                                            <td><input type="text" id="extra_urban_cycl" value="<?php echo $options ? $options->performance->extra_urban_cycl : ''; ?>" role="performance"></td>
                                        </tr>
                                        <tr class="active">
                                            <th colspan="4">CO2排放量</th>
                                        </tr>
                                        <tr>
                                            <th>CO2排放量（城市工况）</th>
                                            <td><input type="text" id="co2_combined_cycle" value="<?php echo $options ? $options->co2->co2_combined_cycle : ''; ?>" role="co2"></td>
                                            <th>CO2排放量（市外工况）</th>
                                            <td><input type="text" id="co2_urban_cycle" value="<?php echo $options ? $options->co2->co2_urban_cycle : ''; ?>" role="co2"></td>
                                        </tr>
                                        <tr>
                                            <th>CO2排放量（混合工况）</th>
                                            <td><input type="text" id="co2_extra_urban_cycl" value="<?php echo $options ? $options->co2->co2_extra_urban_cycl : ''; ?>" role="co2"></td>
                                            <th></th>
                                            <td></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div role="tabpanel" class="tab-pane" id="settings">...</div>
                        </div>

                    </div>
                </div>
                <button type="submit" class="btn btn-default"><?php echo isset($item) && $item ? '保存' : '提交'?>车型信息</button>
            </form>
        </div>
    </div>
    <div class="col-xs-1"></div>
</div>

<div class="modal fade" id="articleModal" tabindex="-1" role="dialog" aria-labelledby="articleModalTitle" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="changePwdModalTitle"> 车型特性资讯选择 </h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>选择</th>
                                <th>资讯标题</th>
                                <th>资讯类别</th>
                                <th>发布人</th>
                                <th>发布时间</th>
                            </tr>
                        </thead>
                        <tbody id="article-list">
                        </tbody>
                        <tfoot>
                            <tr id="loaddingImage" style="display:none;">
                                <td colspan="5" style="text-align: right;"><img src="/assets/img/loading.gif" alt="loading..."/> 数据加载中... </td>
                            </tr>
                            <tr>
                               <td colspan="5" id="article-pagination" style="text-align: right;"></td> 
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="btnChioce" class="btn btn-primary" data-dismiss="modal">选择</button>
          </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="/assets/js/tools.js"></script>
<script type="text/javascript">
    var path = 'article';
    var imgpanel = '';

    function callback(file, data){
        var json = eval('(' + data + ')');
        if(json.status == 'err'){
            return;
        }
        if(imgpanel != 'ace-thumbnails'){
            $("#" + imgpanel).val(json.data);
            return;
        }

        var item = '{"thumbnail" : "' + json.data + '"},';
        $('#images').val($('#images').val() + item);

        $('.ace-thumbnails').append(li, {thumbnail: json.data}, null);
        
    }
</script>
<?php //echo render('ace/public/upload'); ?>
<?php echo render('tools/upload'); ?>

<script type="text/javascript" src="/assets/third-party/editor/kindeditor-min.js"></script>
<script charset="utf-8" src="/assets/third-party/editor/lang/zh_CN.js"></script>
<script type="text/javascript">
$(function(){
    KindEditor.ready(function(K){
        window.editor = K.create('textarea[role="editor"]');
    });
});
</script>

<script type="text/javascript" src="/assets/third-party/jquery-tmpl-master/jquery.tmpl.min.js"></script>
<script type="text/javascript" src="/assets/third-party/jquery-tmpl-master/jquery.tmplPlus.min.js"></script>

<script type="text/x-jquery-tmpl" id="tr">
<tr data-id="${id}" data-title="${title}" data-src="${thumbnail}">
    <td><input type="checkbox" value="${id}"></th>
    <td>${title}</td>
    <td>${category.name}</td>
    <td>${author.name}</td>
    <td>${created_at}</td>
</tr>
</script>

<script type="text/x-jquery-tmpl" id="li">
<li>
    <a href="${url}" data-rel="colorbox" class="cboxElement">
        <img style="width: 150px; height: 150px;" alt="" src="${thumbnail}">
        <div class="text">
            <div class="inner">${desc}</div>
        </div>
    </a>

    <div class="tools tools-bottom">
        <a href="#">
            <i class="ace-icon fa fa-link"></i>
        </a>

        <a href="#">
            <i class="ace-icon fa fa-paperclip"></i>
        </a>

        <a href="#">
            <i class="ace-icon fa fa-pencil"></i>
        </a>

        <a href="#">
            <i class="ace-icon fa fa-times red"></i>
        </a>
    </div>
</li>
</script>

<script type="text/x-jquery-tmpl" id="tabPage">
<div role="tabpanel" class="tab-pane{{if selected}} active {{/if}}" id="tabPage${id}">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>缩略图</th>
                <th>资讯标题</th>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
        {{each nodes}} 
        <tr data-id="{{= $value.id}}" is_new="false">
            <td><a href="{{= $value.thumbnail}}" target="_blank"><img src="{{= $value.thumbnail}}" alt="" style="height: 80px;"></a></td>
            <td>{{= $value.title}}</td>
            <td><a class="btn btn-sm btn-danger" role="delete"><i class="fa fa-trash-o"></i></a></td>
        </tr>
        {{/each}}
        </tbody>
        <tfoot>
            <tr>
                <td colspan="3">
                   <a class="btn btn-sm btn-primary" data-category-id="${id}" data-toggle="modal" data-target="#articleModal"><i class="fa fa-plus"></i></a>
                </td>
            </tr>
        </tfoot>
    </table>
</div>
</script>

<script type="text/x-jquery-tmpl" id="article_item">
<tr data-id="${id}" is_new="${is_new}">
    <td><a href="${src}" target="_blank"><img src="${src}" alt="" style="height: 80px;"></a></td>
    <td>${title}</td>
    <td><a class="btn btn-sm btn-danger" role="delete"><i class="fa fa-trash-o"></i></a></td>
</tr>
</script>

<script type="text/javascript">    
var previous_id = 0;
var current_tabpage;
var current_articles;
var current_indexs;
$(function(){

    //读取车型系列数据
    $.get('/admin/category?cid=<?php echo $seller->car_category; ?>', 
        function(data, status){
            if(data.status == 'err'){
                return;
            }
            $('#category_id').empty();
            items = data.data;
            for (var i = 0; i < items.length; i++) {
                $('#category_id').append('<option value="' + items[i].id + '">' + items[i].name + '</option>');
            };
        }, 'json');

    //读取车型特性属性
    $.get('/admin/category?cid=<?php echo $seller->car_param; ?>', 
        function(data, status){
            if(data.status == 'err'){
                return;
            }

            <?php 
                $nodes = array();
                if(isset($item) && $item){ 
                    foreach ($item->features as $key => $value) {
                        $node = $value->node->to_array();
                        $node['id'] = $value->id;
                        array_push($nodes, $node);
                    }
                } 
             ?>

            var nodes = <?php echo json_encode($nodes); ?>;
            items = data.data;
            for (var i = 0; i < items.length; i++) {
                $('#features_nav').append(' <li role="presentation"' + (i == 0 ? ' class="active"' : '') + '><a href="#tabPage' + items[i].id + '" aria-controls="tabPage' + items[i].id + '" role="tab" data-toggle="tab">' + items[i].name + '</a></li>');
                
                var articles = [];
                for(var j = 0; j < nodes.length; j ++){
                    if(nodes[j].category_id == items[i].id){
                        articles[articles.length] = nodes[j];
                    }
                 }
                $('#features_content').append(tabPage, {id: items[i].id, selected: i == 0, nodes: articles}, null);
            };
        }, 'json');

        //初始化图库
        <?php 
            $images = array();
            if(isset($item) && $item){ 
                foreach ($item->gallery as $key => $value) {
                    array_push($images, $value->to_array());
                }
        ?>
        var images = eval('<?php echo json_encode($images); ?>');
        for (var i = 0; i < images.length; i++) {
            $('.ace-thumbnails').append(li, images[i], null);
        }
        <?php } ?>

    //车型特色资讯关联
    $('#features_content').delegate('a[data-target="#articleModal"]', 'click', function(){
        if(previous_id == $(this).attr('data-category-id')){
            return;
        }
        previous_id = $(this).attr('data-category-id');
        $('#article-list').empty().append('<tr><td colspan="5" style="text-align: center;"><img src="/assets/img/loading.gif" alt="loading..."/> 数据加载中... </td></tr>');
        $.get('/admin/article?category_id=' +  $(this).attr('data-category-id'), 
            function(data, status){
                if(data.status == 'err'){
                    alert(data.msg);
                    return;
                }

                $('#article-list').empty();
                var items = data.data;
                for (var i = 0; i < items.length; i++) {
                    $('#article-list').append(tr, items[i], null);
                }
                $('#article-pagination').html(data.pagination);
                current_articles = [];
                current_indexs = [];
            }, 'json');
    });


    $('a[data-target="#modal-upload"]').click(function(){
        imgpanel = $(this).attr('data-panel');
    });

    //分页业务逻辑
    $('#articleModal').delegate('a[role="page-link"]', 'click', function(){

        $('#loaddingImage').css('display', '');

         $.get($(this).attr('data-href'),
            function(data, status){
                $('#loaddingImage').css('display', 'none');
                if(data.status == 'err'){
                    alert(data.msg);
                    return;
                }

                $('#article-list').empty();
                var items = data.data;
                for (var i = 0; i < items.length; i++) {
                    $('#article-list').append(tr, items[i], null);
                }
                $('#article-pagination').html(data.pagination);
            }, 'json');
    });

    //提交前的操作
    $('button[type="submit"]').click(function(){
        var category = ['overview', 'dimensions', 'tyres', 'engine', 'performance', 'co2'];
        var json = '{';
        for (var i = 0; i < category.length; i++) {

            //组合当前参数分类下的参数
            var item = '{';
            $('input[role="' + category[i] + '"]').each(function(index, element){
                item += '"{0}" : "{1}",'.format($(element).attr('id'), $(element).val());
            });
            item = item.substring(0, item.length - 1);
            item += '}';

            //整体组合
            json += '"{0}" : {1},'.format(category[i], item);
        };
        json = json.substring(0, json.length - 1)
        json += '}';
        $('#options').val(json);

        var json = '[';
        $('#features_content').find('div').each(function(index, element){
            var trs = $(element).find('tbody').find('tr');
            var aids = '';
            for (var i = 0; i < trs.length; i++) {
                if($(trs[i]).attr('is_new') == 'false'){
                    continue;
                }
                aids += $(trs[i]).attr('data-id') + ",";
            }
            aids = aids.substring(0, aids.length - 1);
            json += '{"category": ' + $(element).attr('id').substring(7) + ', "articles":[' + aids + ']},';
        });
        json = json.substring(0, json.length - 1);
        json += ']';

        $('#features').val(json);

        var src = $('#images').val().substring(0, $('#images').val().length - 1);
        $('#images').val("[" + src + "]");
    });

    //添加图像
    $('#btnAddImage').click(function(){
        imgpanel = "ace-thumbnails";
    });

    //添加特性按钮监听
    $('#features_content').delegate('a[data-target="#articleModal"]', 'click', function(){
        current_tabpage = $(this).parents('table').find('tbody');
    });

    //特性删除按钮监听
    $('#features_content').delegate('a[role="delete"]', 'click', function(){
        var a = $(this);
        var i = $(a).find('i');
        var self = $(a).parents('tr');

        $(a).addClass('disabled');
        $(i).addClass('fa-spinner').addClass('fa-spin').removeClass('fa-trash-o');

        $.get('/admin/car/feature?action=delete&id=' + self.attr('data-id'), 
            function(data, status){
                $(a).removeClass('disabled');
                $(i).removeClass('fa-spinner').removeClass('fa-spin').addClass('fa-trash-o');
                if(data.status == 'err'){
                    showResultMessage('删除失败', '', 'danger');
                    return;
                }
                showResultMessage('删除成功', '', 'success');
                self.remove();
            }, 'json');
    });

    //选择资讯的事件
    $('#articleModal').delegate('input[type="checkbox"]', 'click', function(){
        if($(this).is(':checked')){
            for (var i = 0; i < current_indexs.length; i++) {
                if(current_indexs[i] == $(this).parents('tr').attr('data-id')){
                    return;
                }
            }
            var tr = {
                id: $(this).parents('tr').attr('data-id'),
                title: $(this).parents('tr').attr('data-title'),
                src: $(this).parents('tr').attr('data-src'),
                is_new: true
            };
            current_articles[current_articles.length] = tr;
            current_indexs[current_indexs.length] = $(this).parents('tr').attr('data-id');
        }else{
            for (var i = 0; i < current_indexs.length; i++) {
                if(current_indexs[i] == $(this).parents('tr').attr('data-id')){
                    current_indexs.remove(i);
                    current_articles.remove(i);
                    break;
                }
            }
        }
    });

    //确认最终选择的资讯
    $('#btnChioce').click(function(){
        var trs = $(current_tabpage).find('tr');
        var ids = [];
        for (var i = 0; i < trs.length; i++) {
            ids[ids.length] = $(trs[i]).attr('data-id');
        }
        for (var i = 0; i < current_articles.length; i++) {
            if(ids.is_exist(current_articles[i].id)){
                continue;
            }
            $(current_tabpage).append(article_item, current_articles[i], null);
        }
    });
});
</script>

